//
//  BIDConstructionDetailViewController.m
//  JuanChengOA
//
//  Created by mal on 2017/7/28.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDConstructionDetailViewController.h"
#import "BIDApplyReviewCell.h"
#import "BIDApplyDetailHeaderView.h"
#import "BIDBrowserImageViewController.h"
#import "BIDCustomTextView.h"

static NSString *detailURL = @"itemBuildingAndroid!itemBuildingDetail.action";
//回复
static NSString *replyURL = @"itemBuildingAndroid!replyItemBuilding.action";

@interface BIDConstructionDetailViewController ()<UITableViewDelegate, UITableViewDataSource, BIDApplyDetailHeaderViewDelegate, UITextViewDelegate>
{
    NSArray *_dataSourceArr;
    BIDApplyReviewCell *_tempCell;
    BOOL _isRegister;
    BIDApplyDetailHeaderView *_headerView;
    NSDictionary *_detailDic;
}
@end

@implementation BIDConstructionDetailViewController
@synthesize itemID;
@synthesize replyID;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"详情";
    //
    _tableView.tableFooterView = [[UIView alloc] init];
    _dataSourceArr = @[];
    _tempCell = [[[NSBundle mainBundle] loadNibNamed:@"BIDApplyReviewCell" owner:self options:nil] lastObject];
    //
    _headerView = [[[NSBundle mainBundle] loadNibNamed:@"BIDApplyDetailHeaderView" owner:self options:nil] lastObject];
    _headerView.delegate = self;
    //
    _messageTextView.placeholder = @"请输入回复内容……";
    _messageTextView.delegate = self;
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], detailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@&replyId=%@", itemID, replyID];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            NSArray *arr = responseDic[@"itemBuilding"][@"itemReplys"];
            [_headerView prepareWithDic:responseDic[@"itemBuilding"]];
            _dataSourceArr = [[NSMutableArray alloc] initWithArray:arr];
            _detailDic = [[NSDictionary alloc] initWithDictionary:responseDic[@"itemBuilding"]];
        }
        [_tableView reloadData];
    }];
}

/**
 留言
 */
- (void)toReply
{
    NSString *strMsg = _messageTextView.text;
    if(strMsg.length == 0)
    {
    }
    else
    {
    }
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], replyURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"itemReplyContent.itemBuildingId=%@&itemReplyContent.replyContent=%@&itemReplyContent.replyUserId=%@",itemID, strMsg, [AppDelegate getUserId]];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            _messageTextView.text = @"";
            [_messageTextView resignFirstResponder];
            [self loadData];
        }
    }];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    self.keyboardHeight = keyboardSize.height;
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.25f];
    _bottomForMessageViewConstraint.constant = self.keyboardHeight;
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.30f];
    _bottomForMessageViewConstraint.constant = 0;
    [UIView commitAnimations];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
    CGFloat height = textView.contentSize.height;
    if(height >= 89)
    {
        _heightForMessageViewConstraint.constant = 89;
    }
    else
    {
        _heightForMessageViewConstraint.constant = height+10>40 ? height+10 : 40;
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if([text isEqualToString:@"\n"])
    {
        [self toReply];
        return NO;
    }
    else
    {
        return YES;
    }
}

#pragma mark - BIDApplyDetailHeaderViewDelegate
- (void)updateHeaderView
{
    _tableView.tableHeaderView = _headerView;
}

- (void)showPhotoAtIndex:(NSInteger)index withImgArr:(NSArray*)imgArr;
{
    BIDBrowserImageViewController *vc = [[BIDBrowserImageViewController alloc] initWithNibName:@"BIDBrowserImageViewController" bundle:nil];
    vc.index = index;
    vc.imgArr = [[NSArray alloc] initWithArray:imgArr];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_isRegister)
    {
        _isRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDApplyReviewCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDApplyReviewCell *reviewCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = _dataSourceArr[row];
    reviewCell.nameLabel.text = [[NSString alloc] initWithFormat:@"%@", dic[@"replyUserName"]];
    reviewCell.timeLabel.text = dic[@"replyTime"];
    reviewCell.contentLabel.text = dic[@"replyContent"];
    cell = reviewCell;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;
    _tempCell.contentLabel.preferredMaxLayoutWidth = CGRectGetWidth(tableView.frame)-16-8;
    NSDictionary *dic = _dataSourceArr[row];
    _tempCell.contentLabel.text = dic[@"reviewContent"];
    CGSize size = [_tempCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    CGFloat height = size.height > 84 ? size.height+1 : 84;
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(_dataSourceArr.count>0) return 35.f;
    else return 0.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 35.f)];
    [headerView setBackgroundColor:RGBACOLOR(226, 233, 243, 1)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 35)];
    [label setText:@"  回复内容"];
    [label setTextColor:MO_COLOR];
    [label setFont:[UIFont systemFontOfSize:14.f]];
    [headerView addSubview:label];
    return headerView;
}


@end
